import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;


/**
 * socketServer
 * xyz.socketserver.server
 * Created by Blue on 2021/11/9.
 */
@Slf4j
public class NIOClient {
	
	public static void main(String[] args) throws IOException, InterruptedException {
		
		InetSocketAddress socketAddress = new InetSocketAddress("0.0.0.0", 10002);
		SocketChannel socketChannel = SocketChannel.open(socketAddress);
		
		log.info("连接 BIOServer 服务，端口：10002...");
		
		ArrayList<String> companyDetails = new ArrayList<>();
		
		// 创建消息列表
		companyDetails.add("腾讯");
		companyDetails.add("阿里巴巴");
		companyDetails.add("京东");
		companyDetails.add("百度");
		companyDetails.add("google");
		
		for (String companyName : companyDetails) {
			socketChannel.write(ByteBuffer.wrap(companyName.getBytes()));
			log.info("发送: " + companyName);
			
			int BUFF_SIZE = 1024;
			ByteBuffer buffer = ByteBuffer.allocate(BUFF_SIZE);
			buffer.clear();
			socketChannel.read(buffer);
			String result = new String(buffer.array()).trim();
			log.info("收到NIOServer回复的消息：" + result);
			
			// 等待2秒钟再发送下一条消息
			Thread.sleep(2000);
		}
		
		socketChannel.close();
	}
}
